package com.kreao.korean.mapper;

import com.kreao.korean.dto.WordRequest;
import com.kreao.korean.dto.WordResponse;
import com.kreao.korean.entity.Word;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 单词实体和DTO之间的映射器
 */
@Component
public class WordMapper {

    /**
     * 将WordRequest转换为Word实体
     */
    public Word toEntity(WordRequest request) {
        if (request == null) {
            return null;
        }

        Word word = new Word();
        word.setKorean(request.getKorean());
        word.setChinese(request.getChinese());
        word.setPronunciation(request.getPronunciation());
        word.setRomanization(request.getRomanization());
        word.setExamples(request.getExamples());
        word.setNotes(request.getNotes());
        word.setCategory(request.getCategory());
        word.setLevel(request.getLevel());
        word.setWordType(request.getWordType());
        word.setIsFavorite(request.getIsFavorite() != null ? request.getIsFavorite() : false);
        word.setIsPublic(request.getIsPublic() != null ? request.getIsPublic() : false);
        
        // 设置默认值
        word.setReviewCount(0);
        word.setCorrectCount(0);
        word.setWrongCount(0);
        word.setAccuracy(0.0);
        word.setNextReviewTime(LocalDateTime.now().plusDays(1));

        return word;
    }

    /**
     * 将Word实体转换为WordResponse
     */
    public WordResponse toResponse(Word word) {
        if (word == null) {
            return null;
        }

        WordResponse response = new WordResponse();
        response.setId(word.getId());
        response.setKorean(word.getKorean());
        response.setChinese(word.getChinese());
        response.setPronunciation(word.getPronunciation());
        response.setRomanization(word.getRomanization());
        response.setExamples(word.getExamples());
        response.setNotes(word.getNotes());
        response.setCategory(word.getCategory());
        response.setLevel(word.getLevel());
        response.setWordType(word.getWordType());
        response.setIsFavorite(word.getIsFavorite());
        response.setIsPublic(word.getIsPublic());
        response.setReviewCount(word.getReviewCount());
        response.setCorrectCount(word.getCorrectCount());
        response.setWrongCount(word.getWrongCount());
        response.setAccuracy(word.getAccuracy());
        response.setLastReviewTime(word.getLastReviewTime());
        response.setNextReviewTime(word.getNextReviewTime());
        response.setCreatedAt(word.getCreatedAt());
        response.setUpdatedAt(word.getUpdatedAt());

        return response;
    }

    /**
     * 将Word实体列表转换为WordResponse列表
     */
    public List<WordResponse> toResponseList(List<Word> words) {
        if (words == null) {
            return null;
        }

        return words.stream()
                .map(this::toResponse)
                .collect(Collectors.toList());
    }

    /**
     * 更新现有实体的字段
     */
    public void updateEntity(Word existingWord, WordRequest request) {
        if (existingWord == null || request == null) {
            return;
        }

        existingWord.setKorean(request.getKorean());
        existingWord.setChinese(request.getChinese());
        existingWord.setPronunciation(request.getPronunciation());
        existingWord.setRomanization(request.getRomanization());
        existingWord.setExamples(request.getExamples());
        existingWord.setNotes(request.getNotes());
        existingWord.setCategory(request.getCategory());
        existingWord.setLevel(request.getLevel());
        existingWord.setWordType(request.getWordType());
        
        if (request.getIsFavorite() != null) {
            existingWord.setIsFavorite(request.getIsFavorite());
        }
        
        if (request.getIsPublic() != null) {
            existingWord.setIsPublic(request.getIsPublic());
        }
    }
} 